package com.zues.thread.lock.dead;

import lombok.extern.slf4j.Slf4j;

/**
 * 死锁演示
 * @author zues
 * @date 12/7/2024 11:33 下午
 */
@Slf4j(topic = "c.DeadLockTest")
public class DeadLockTest {

    public static void main(String[] args) {
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        new Thread(()->{
            synchronized (s1){
                s1.append("a");
                s2.append("1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (s2){
                    s1.append("b");
                    s1.append("1");
                    log.info("{} s1 = ", s1);
                    log.info("{} s2 = ", s2);
                }
            }
        }).start();


        new Thread(()->{
            synchronized (s2){
                s1.append("b");
                s2.append("2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (s1){
                    s1.append("a");
                    s1.append("2");
                    log.info("{} s1 = ", s1);
                    log.info("{} s2 = ", s2);
                }
            }
        }).start();


    }


}
